package com.jade.linkong.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.jade.common.utils.DateUtils;
import com.jade.common.utils.StringUtils;

import com.jade.linkong.domain.LkTemplate;
import com.jade.linkong.mapper.LkTemplateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jade.linkong.mapper.LkMatterMapper;
import com.jade.linkong.mapper.LkMatterTypeMapper;
import com.jade.linkong.domain.LkMatter;
import com.jade.linkong.domain.LkMatterType;
import com.jade.linkong.service.ILkMatterTypeService;
import com.jade.system.domain.SysDept;
import com.jade.system.domain.SysUser;
import com.jade.common.annotation.DataScope;
import com.jade.common.constant.UserConstants;
import com.jade.common.core.domain.Ztree;
import com.jade.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 事项类型Service业务层处理
 * 
 * @author jade
 * @date 2020-07-16
 */
@Service
public class LkMatterTypeServiceImpl implements ILkMatterTypeService 
{
    @Autowired
    private LkMatterTypeMapper lkMatterTypeMapper;
    @Autowired
    private LkMatterMapper lkMatterMapper;
    @Resource
    private LkTemplateMapper lkTemplateMapper;

    
    /**
     * 查询事项类型
     * 
     * @param typeId 事项类型ID
     * @return 事项类型
     */
    @Override
    public LkMatterType selectLkMatterTypeById(Long typeId)
    {
        return lkMatterTypeMapper.selectLkMatterTypeById(typeId);
    }

    /**
     * 查询事项类型列表
     * 
     * @param lkMatterType 事项类型
     * @return 事项类型
     */
    @Override
    public List<LkMatterType> selectLkMatterTypeList(LkMatterType lkMatterType)
    {
        return lkMatterTypeMapper.selectLkMatterTypeList(lkMatterType);
    }

    /**
     * 新增事项类型
     * 
     * @param lkMatterType 事项类型
     * @return 结果
     */
    @Override
    public int insertLkMatterType(LkMatterType lkMatterType)
    {
        lkMatterType.setCreateTime(DateUtils.getNowDate());
        return lkMatterTypeMapper.insertLkMatterType(lkMatterType);
    }

    /**
     * 修改事项类型
     * 
     * @param lkMatterType 事项类型
     * @return 结果
     */
    @Override
    public int updateLkMatterType(LkMatterType lkMatterType)
    {
        lkMatterType.setUpdateTime(DateUtils.getNowDate());
        return lkMatterTypeMapper.updateLkMatterType(lkMatterType);
    }

    /**
     * 删除事项类型对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteLkMatterTypeByIds(String ids)
    {
        return lkMatterTypeMapper.deleteLkMatterTypeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除事项类型信息
     * 
     * @param typeId 事项类型ID
     * @return 结果
     */
    public int deleteLkMatterTypeById(Long typeId)
    {
        return lkMatterTypeMapper.deleteLkMatterTypeById(typeId);
    }


    /**
     * 校验分类名称是否唯一
     * 
     * @param lkMatterType 分类信息
     * @return 结果
     */
    @Override
    public String checkLkMatterTypNameUnique(LkMatterType lkMatterType)
    {
        Long typeId = StringUtils.isNull(lkMatterType.getTypeId()) ? -1L : lkMatterType.getTypeId();
        LkMatterType info = lkMatterTypeMapper.checkLkMatterTypNameUnique(lkMatterType.getTypeName(), lkMatterType.getParentId());
        if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != typeId.longValue())
        {
            return UserConstants.DEPT_NAME_NOT_UNIQUE;
        }
        return UserConstants.DEPT_NAME_UNIQUE;
    }
    

    /**
     * 查询所有分类
     * 
     * @return 分类列表
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<Ztree> treeData()
    {
        List<LkMatterType> types = lkMatterTypeMapper.selectLkMatterTypeList(new LkMatterType());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (LkMatterType type : types)
        {
                Ztree ztree = new Ztree();
                ztree.setId(type.getTypeId());
                ztree.setpId(type.getParentId());
                ztree.setName(type.getTypeName());
                ztree.setTitle(type.getTypeName());
                ztrees.add(ztree);
        }
        return ztrees;
    }
    


	@Override
	public int selectTypeCount(Long parentId) {
		LkMatterType type = new LkMatterType();
		type.setParentId(parentId);
        return lkMatterTypeMapper.selectTypeCount(type);
	}
    
	@Override
    public List<Ztree> selectMatterTypeTree(Integer tempId)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        List<LkMatterType> types = lkMatterTypeMapper.selectLkMatterTypeList(new LkMatterType());
        if (StringUtils.isNotNull(tempId))
        {
            LkTemplate template = lkTemplateMapper.selectLkTemplateById(tempId.toString());
            ztrees = initZtree(types, template.getMatterIds());
        }
        else
        {
            ztrees = initZtree(types,null);
        }
        return ztrees;
    }

    /**
     * 对象转部门树
     *
     * @param typeList 部门列表
     * @param
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<LkMatterType> typeList,String matterIds)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        List<String> list = new ArrayList<>();
        boolean isCheck = StringUtils.isNotNull(matterIds);
        if (isCheck){
            list = Arrays.asList(matterIds.split(","));
        }
        for (LkMatterType type : typeList)
        {
                Ztree ztree = new Ztree();
                LkMatter matter = new LkMatter();
                matter.setTypeId(type.getTypeId());
                List<LkMatter> matterList = lkMatterMapper.selectLkMatterList(matter);
                ztree.setpId(type.getParentId());
                ztree.setId(type.getTypeId());
                ztree.setName(type.getTypeName());
                ztree.setTitle(type.getTypeName());
                if(StringUtils.isNotEmpty(matterList)){
                    for(LkMatter u: matterList){
                        Ztree ztree1 = new Ztree();
                        ztree1.setId(u.getId());
                        ztree1.setpId(type.getTypeId());
                        ztree1.setName(u.getName());
                        ztree1.setTitle(u.getName());
                        ztree1.setText("事项");
                        if (isCheck)
                        {
                            if (list.contains(u.getId()+"")) {
                                ztree1.setChecked(true);
                                ztree.setChecked(true);
                            }
                        }
                        ztrees.add(ztree1);
                    }
                }
            ztrees.add(ztree);
            }
        return ztrees;
    }
}
